library(haven)
library(readstata13)
library(plm)
library(AER)
library(foreign)
library(corrgram)
library(reshape)
library(ggplot2)
library(ggthemes)
library(Rarity)

#Figure 1
donate <- read.dta13("Barber_Eatough_Replication_JOP.dta")

max <- tapply(X = donate$politicization, INDEX = donate$INDUSTRY, FUN = function(x) max(x, na.rm = T))
min <- tapply(X = donate$politicization, INDEX = donate$INDUSTRY, FUN = function(x) min(x, na.rm = T))
median <- tapply(X = donate$politicization, INDEX = donate$INDUSTRY, FUN = function(x) median(x, na.rm = T))

together <- as.data.frame(cbind(max, min, median, names(min)))
together$max <- as.numeric(as.character(together$max))
together$min <- as.numeric(as.character(together$min))
together$median <- as.numeric(as.character(together$median))
names(together) <- c("max", "min", "median", "name")
together <- together[order(together$min),]
together$yaxis <- rank(together$min)

par(mar = c(4, 10, 4, 2))
plot(together$min, together$yaxis, pch=15, cex=1, main = "", xlab="Politicization Score" , bty="n", ylab=NA, yaxt="n", xaxt="n", las=1, xlim = c(0, 100), ylim=c(0,76))
points(together$max, together$yaxis, pch=15, cex=1)
segments(x0 = together$min, x1 = together$max, y0 = together$yaxis, y1 = together$yaxis)
points(together$median, together$yaxis, pch = 16, cex = .7)
ytick <- seq(1,75, by=1)
axis(side=2, at=ytick, labels=FALSE)
axis(side=1, at=seq(0, 100, 20), labels=seq(0, 100, 20))
text(par("usr")[1], ytick, labels=together$name, pos=2, xpd=TRUE, cex=0.5)
abline(v = seq(0, 100, 10), col = "grey", lty = 2)
box()
mtext(side = 1, at = 0, "Least\nPoliticized", cex = .75, line = 2.5)
mtext(side = 1, at = 100, "Most\nPoliticized", cex = .75, line = 2.5)
mtext(side = 3, at = 50, line = 1, "Industry Politicization (Minimum and Maximum Scores)")

summary(donate$politicization)
############################################
#Figures 2a and 2b
#within versus across industry variance

with <- lm(politicization ~ as.factor(CODE5A), data = donate)
within_resid <- resid(with)
within_var <- sum(within_resid^2)/(length(within_resid) - length(unique(donate$CODE5A)))
total_var <- var(donate$politicization)
between <- total_var - within_var

within_var/total_var
#0.1260406
between/total_var
#0.8739594

donate00 <- donate[donate$CYCLE == 2000,]
donate00 <- donate00[order(donate00$CODE5A),]
donate00$N <- unlist(tapply(donate00$CODE5A, donate00$CODE5A, function(x) 1:length(x)))
donate00 <- donate00[donate00$N == 1,]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2002])
table(is.na(m))
donate00$score02 <- donate$politicization[donate$CYCLE == 2002][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2004])
table(is.na(m))
donate00$score04 <- donate$politicization[donate$CYCLE == 2004][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2006])
table(is.na(m))
donate00$score06 <- donate$politicization[donate$CYCLE == 2006][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2008])
table(is.na(m))
donate00$score08 <- donate$politicization[donate$CYCLE == 2008][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2010])
table(is.na(m))
donate00$score10 <- donate$politicization[donate$CYCLE == 2010][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2012])
table(is.na(m))
donate00$score12 <- donate$politicization[donate$CYCLE == 2012][m]

m <- match(donate00$CODE5A, donate$CODE5A[donate$CYCLE == 2014])
table(is.na(m))
donate00$score14 <- donate$politicization[donate$CYCLE == 2014][m]

## Correlation Matrices
corr.matrix <- subset(donate00, select = c("CYCLESCORE", "score02", "score04", "score06", 
                                           "score08","score10","score12","score14"))
names(corr.matrix) <- c("yr2000", "yr2002", "yr2004", "yr2006", "yr2008", "yr2010", "yr2012", "yr2014")

cor.matrix <- round(cor(corr.matrix, use = "complete.obs", method = "spearman"), digits = 2)
cor.matrix[2,1] <- NA
cor.matrix[3,1:2] <- NA
cor.matrix[4,1:3] <- NA
cor.matrix[5,1:4] <- NA
cor.matrix[6,1:5] <- NA
cor.matrix[7,1:6] <- NA
cor.matrix[8,1:7] <- NA

cor.matrix <- abs(cor.matrix)
cor.matrix
cor.dat <- melt(cor.matrix)
cor.dat <- cor.dat[-which(is.na(cor.dat[, 3])),]
cor.dat <- data.frame(cor.dat)
cor.dat

levels(cor.dat$X1) <- list("yr2000"="yr1", "yr2002"="yr2", "yr2004"="yr3", "yr2006"="yr4", "yr2008"="yr5", "yr2010"="yr6", "yr2012"="yr7", "yr2014"="yr8")
levels(cor.dat$X2) <- rev(list("yr2000"="yr1", "yr2002"="yr2", "yr2004"="yr3", "yr2006"="yr4", "yr2008"="yr5", "yr2010"="yr6", "yr2012"="yr7", "yr2014"="yr8"))
theme_set(theme_bw())

#Figure 2a
ggplot(cor.dat, aes(X2, X1, fill = value)) + 
  geom_tile() + ggtitle("Cycle-to-Cycle Correlation of Politicization Measure") +
  geom_text(aes(X2, X1, label = value), color = "black", size = 4) +
  scale_fill_gradient(name=expression("Spearman" * ~ rho), low = "#ffffff", high = "#045975", breaks=seq(0, 1, by = 0.2), limits = c(0, 1)) +
  scale_x_discrete(labels=c("2000", "2002", "2004", "2006", "2008", "2010", "2012", "2014"), expand = c(0, 0)) +
  scale_y_discrete(labels=rev(c("2000", "2002", "2004", "2006", "2008", "2010", "2012", "2014")), expand = c(0, 0)) +
  labs(x = "", y = "") + 
  guides(fill = guide_colorbar(barwidth = 10, barheight = 1, title.position = "top", title.hjust = 0.5)) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1), 
        panel.grid.major = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks = element_blank(),
        legend.justification = c(1, 0),
        legend.position = c(0.9, 0.7),
        legend.direction = "horizontal")

mean(cor.dat$value[cor.dat$value != 1])

#Figure 2b - correlations between various robustness measures
data <- read.csv("correlations.csv")
data$Issue.Name. <- as.character(data$Issue.Name.)

corrs <- subset(data, select = c("orig", "lib_cons", "cong_leg", "policies_bill", "usatoday", "wsj"))
names(corrs) <- c("Original", "Liberal/\nConservative", "Congress/\nLegislature", "Policies/\nBill", "USA Today", "WSJ")

summary(corrs$Original)
summary(corrs$`Liberal/Conservative`)
summary(corrs$`Congress/Legislature`)
summary(corrs$`Policies/Bill`)
summary(corrs$`USA Today`)
summary(corrs$WSJ)

#correlation between all four methods
corPlot(corrs, method = "spearman", pch = 4, xlab = "", ylab = "")


##########################################################################
#Figures 3-6

percentilerank<-function(x){
  rx<-rle(sort(x))
  smaller<-cumsum(c(0, rx$lengths))[seq(length(rx$lengths))]
  larger<-rev(cumsum(c(0, rev(rx$lengths))))[-1]
  rxpr<-smaller/(smaller+larger)
  rxpr[match(x, rx$values)]
}

#Figures 3a and 3b - Share non-incumbent giving
hist(donate$share_nonincumbent[donate$numberofdonations > 4 & !is.na(donate$politicization)], 
     xlab = "Share of Donations Given to Non-Incumbent Candidates", col = "grey", 
     main = "Giving to Non-Incumbent Candidates", axes = F)
axis(1, at = seq(0, 100, 20))
axis(2, at = seq(0, 6000, 1000), las = 2)
box()

data3 <- donate
data3 <- data3[data3$numberofdonations > 4,]
data3 <- data3[!is.na(data3$share_nonincumbent),]
data3 <- data3[!is.na(data3$politicization),]
data3 <- data3[order(data3$politicization),]
plx <- predict(loess(share_nonincumbent ~ politicization, data = data3), se = T)
data3$donation.pctile <- percentilerank(data3$donations)

# More ideological, more likely to support challengers
plot(data3$politicization, data3$share_nonincumbent, pch = 16, cex = data3$donation.pctile*3, 
     col = "#00000010", ylim = c(0, 100),
     xlab = "Industry Politicization Score", 
     ylab = "Share of Donations Going To Non-Incumbents", main = "Giving to Non-Incumbent Candidates",
     axes = F)
lines(data3$politicization, plx$fit, lwd = 4, col = "dark red")
text(45, 90, "Larger Dots = More Money Donated", cex = .75)
axis(1, at = seq(0, 70, 10))
axis(2, at = seq(0, 100, 20), las = 2)
box()


#Figures 4a and 4b - district Competitiveness
hist(donate$avgdistrictmargin[donate$numberofdonations > 4 & !is.na(donate$politicization)], 
     xlab = "Average Prior Vote Margin of Districts Donated To", 
     col = "grey", main = "Competitiveness of Races Given To", axes = F, breaks = 20,
     ylim = c(0, 5000))
axis(1, at = seq(0, 100, 10))
axis(2, at = seq(0, 5000, 1000), las = 2)
box()

data3 <- donate
data3 <- data3[data3$numberofdonations > 4,]
data3 <- data3[!is.na(data3$avgdistrictmargin),]
data3 <- data3[!is.na(data3$politicization),]
data3 <- data3[order(data3$politicization),]
plx <- predict(loess(avgdistrictmargin ~ politicization, data = data3), se = T)
data3$donation.pctile <- percentilerank(data3$donations)

# More ideological, more likely to support competitive races
plot(data3$politicization, data3$avgdistrictmargin, pch = 16, cex = data3$donation.pctile*3, col = "#00000010", 
     ylim = c(10, 50), xlab = "Industry Politicization Score", 
     ylab = "Average Prior Vote Margin of Recipient Races", main = "Competitiveness of Recipient Races",
     axes = F)
lines(data3$politicization, plx$fit, lwd = 4, col = "dark red")
text(55, 15, "Larger Dots = More Money Donated", cex = .75)
axis(1, at = seq(0, 70, 10))
axis(2, at = seq(10, 50, 10), las = 2) 
box()
summary(data3$avgdistrictmargin)


#Figures 5a and 5b - Share of donations to majority party
hist(donate$share_majority[donate$numberofdonations > 4 & !is.na(donate$politicization)], 
     xlab = "Share of Donations Given to Majority Party Candidates", col = "grey", 
     main = "Giving to Majority Party Candidates", axes = F)
axis(1, at = seq(0, 100, 20))
axis(2, at = seq(0, 2000, 500), las = 2)
box()

data3 <- donate
data3 <- data3[data3$numberofdonations > 4,]
data3 <- data3[!is.na(data3$share_majority),]
data3 <- data3[!is.na(data3$politicization),]
data3 <- data3[order(data3$politicization),]
plx <- predict(loess(share_majority ~ politicization, data = data3), se = T)
data3$donation.pctile <- percentilerank(data3$donations)

# More ideological, more likely to support minority
plot(data3$politicization, data3$share_majority, pch = 16, cex = data3$donation.pctile*3, 
     col = "#00000010", ylim = c(0, 100), xlab = "Industry Politicization Score", 
     ylab = "Share of Donations To Majority Party Candidates", main = "Giving To Majority Party",
     axes = F)
lines(data3$politicization, plx$fit, lwd = 4, col = "dark red")
text(55, 20.3, "Larger Dots = More Money Donated", cex = .75)
axis(1, at = seq(0, 70, 10))
axis(2, at = seq(0, 100, 20), las = 2)
box()



#Figures 6a and 6b - Share of money to committee chairs
hist(donate$share_chair[donate$numberofdonations > 4 & !is.na(donate$politicization)], 
     xlab = "Share of Donations Given to Committee Chairs", col = "grey", 
     main = "Giving to Committee Chairs", axes = F)
axis(1, at = seq(0, 100, 20))
axis(2, at = seq(0, 7000, 1000), las = 2)
box()

data3 <- donate
data3 <- data3[data3$numberofdonations > 4,]
data3 <- data3[!is.na(data3$share_chair),]
data3 <- data3[!is.na(data3$politicization),]
data3 <- data3[order(data3$politicization),]
plx <- predict(loess(share_chair ~ politicization, data = data3), se = T)
data3$donation.pctile <- percentilerank(data3$donations)

# More ideological, less likely to support chair
plot(data3$politicization, data3$share_chair, pch = 16, cex = data3$donation.pctile*3, 
     col = "#00000005", ylim = c(0, 32), axes = F,
     xlab = "Industry Politicization Score", 
     ylab = "Share of Donations To Committee Chairs", main = "Giving To Committee Chairs")
lines(data3$politicization, plx$fit, lwd = 4, col = "dark red")
text(55, 30, "Larger Dots = More Money Donated", cex = .75)
axis(1, at = seq(0, 70, 10))
axis(2, at = seq(0, 30, 5), las = 2)
box()





